{% extends 'users/user-base.html' %}

{% load static %}
{% block title %}端口审核{% endblock %}
{% block custom_css %}
    {#    <link href="css/domain.css" rel="stylesheet" title="" type="text/css"/>#}
    <style>
        td{
            vertical-align:middle !important;
        }
    </style>
{% endblock %}

{% block custom_js %}{% endblock %}

{% block contentVMport %}
    <script type="text/javascript">
        $(document).ready(function () {
            $("#tab").on("click", ":button", function (event) {
                console.log($(this).attr('id'));
                var btn_id = $(this).attr('id');
                var apply_id = $(this).closest("tr").find("td").eq(0).text();
                console.log(apply_id);
                if (btn_id == 'agree_port') {
                    $.ajax({
                        type: "POST",
                        data: {'applyID': apply_id},
                        url: "/admins/agree_port/",
                        success: function (data) {
                            if (data["st"] === 1) {
                                alert("成功");
                                window.location.reload();
                            } else if (data["st"] === 2) {
                                alert("失败:不能连接到服务器");
                            } else if (data["st"] === 7) {
                                alert("失败:配置修改失败");
                            }
                        },
                        error: function () {
                            alert("失败");
                        }
                    })
                } else if (btn_id == 'refuse_port') {
                    $.ajax({
                        type: "POST",
                        data: {'applyID': apply_id},
                        url: "/admins/refuse_port/",
                        success: function (data) {
                            if (data["st"] === 1) {
                                alert("成功");
                                window.location.reload();
                            }
                        },
                        error: function () {
                            alert("失败");
                        }
                    })
                } else if (btn_id == 'delete_port') {
                    $.ajax({
                        type: "POST",
                        data: {'applyID': apply_id},
                        url: "/admins/delete_port/",
                        success: function (data) {
                            if (data["st"] === 1) {
                                alert("成功");
                                window.location.reload();
                            }
                        },
                        error: function () {
                            alert("失败");
                        }
                    })
                }

            })
        })

    </script>
    <div class="dashboard-wrapper">
            <div class="dashboard-ecommerce">
                <div class="container-fluid dashboard-content ">
                    <!-- ============================================================== -->
                    <!-- pageheader  -->
                    <!-- ============================================================== -->
                    <div class="row">
                        <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
                            <div class="page-header">
                                <h2 class="pageheader-title">端口审核</h2>
                                <div class="page-breadcrumb">
                                    <nav aria-label="breadcrumb">
                                        <ol class="breadcrumb">
                                            <li class="breadcrumb-item">虚拟机设置</a></li>
                                            <li class="breadcrumb-item active" aria-current="page">端口审核</li>
                                        </ol>
                                    </nav>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- ============================================================== -->
                    <!-- end pageheader  -->
                    <!-- ============================================================== -->

                    <div class="row">
                        <!-- ============================================================== -->
                        <!-- basic table  -->
                        <!-- ============================================================== -->
                        <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
                            <div class="card">
                                <div class="card-body">
                                    <div class="table-responsive">
                                        <table class="table table-striped table-bordered first" id="tab">
                                            <thead>
                                            <tr>
                                                <th class="table-id">ID</th>
                                                <th >用户名</th>
                                                <th >虚拟机</th>
                                                <th >操作系统</th>
                                                <th >区域</th>
                                                <th >申请端口</th>
                                                <th >映射端口</th>
                                                <th >申请时间</th>
                                                <th >操作</th>
                                            </tr>
                                            </thead>
                                            {% for apply in data %}
                                                <tr>
                                                    <td> {{ apply.id }}</td>
                                                    <td> {{ apply.user.username }}</td>
                                                    <td> {{ apply.vm.name }}</td>
                                                    <td> {{ apply.vm.os.os_version }}</td>
                                                    <td> {{ apply.vm.region.domain }}</td>
                                                    <td> {{ apply.vmPort }}</td>
                                                    <td> {{ apply.mapPort }}</td>
                                                    <td> {{ apply.apply_time }}</td>
                                                    {% if apply.state == 0 %}
                                                        <input type="hidden" id="agreevm" name="vm_id"
                                                               value={{ apply.id }}>
                                                        <td>
                                                            <button type="button" class="btn btn-success"
                                                                    id="agree_port">
                                                                <span class="glyphicon glyphicon-ok"></span>
                                                                同意
                                                            </button>
                                                            <button type="button" class="btn btn-danger"
                                                                    id="refuse_port">
                                                                <span class="glyphicon glyphicon-remove"></span>
                                                                拒绝
                                                            </button>
                                                        </td>
                                                    {% elif apply.state == 1 %}
                                                        <td>
                                                            <button type="button" class="btn btn-info">
                                                                <span class="glyphicon glyphicon-upload"></span>
                                                                已同意
                                                            </button>
                                                            <button type="button" class="btn btn-default"
                                                                    id="delete_port" style="background-color:#ff0019">
                                                                <span class="glyphicon glyphicon-ban-circle"></span>
                                                                删除
                                                            </button>
                                                        </td>
                                                    {% elif apply.state == 2 %}
                                                        <td>
                                                            <button type="button" class="btn btn-warning">
                                                                <span class="glyphicon glyphicon-download"></span>
                                                            已拒绝
                                                            </button>
                                                        </td>
                                                    {% elif apply.state == 3 %}
                                                        <td>
                                                            <button type="button" class="btn btn-primary">
                                                                <span class="glyphicon glyphicon-trash"></span>
                                                                已删除
                                                            </button>
                                                        </td>
                                                    {% endif %}
                                                </tr>
                                            {% endfor %}
                                        </table>
                                        {% if data.paginator.num_pages > 1%}
                                            <nav aria-label="Page navigation example">
                                                <ul class="pagination pull-right" style="padding-right: 50px;">
                                                    {% if data.number == 1 %}
                                                        <li class="disabled page-item">
                                                            <a aria-label="First" class="page-link">
                                                                <span aria-hidden="true">&laquo;</span>
                                                            </a>
                                                        </li>
                                                    {% else %}
                                                        <li class="page-item">
                                                            <a href="{% url 'admins:apply_port' 1 %}" aria-label="First" class="page-link">
                                                                <span aria-hidden="true">&laquo;</span>
                                                            </a>
                                                        </li>
                                                    {% endif %}
                                                    {% if data.has_previous %}
                                                        <li class="page-item">
                                                            <a href="{% url 'admins:apply_port' data.previous_page_number %}" aria-label="Previous" class="page-link">
                                                                <span aria-hidden="true">&lt;</span>
                                                            </a>
                                                        </li>
                                                    {% else %}
                                                        <li class="disabled page-item">
                                                            <a aria-label="Previous" class="page-link">
                                                                <span aria-hidden="true">&lt;</span>
                                                            </a>
                                                        </li>
                                                    {% endif %}
                                                    {% for num in display_list %}
                                                        {% if data.number == num %}
                                                            <li class="active page-item">
                                                                <a class="page-link">
                                                                    <span aria-hidden="true">{{num}}</span>
                                                                </a>
                                                            </li>
                                                        {% else %}
                                                            <li class="page-item">
                                                                <a href="{% url 'admins:apply_port' num %}" class="page-link">
                                                                    <span aria-hidden="true">{{num}}</span>
                                                                </a>
                                                            </li>
                                                        {% endif %}
                                                    {% endfor %}
                                                    {% if data.has_next %}
                                                        <li class="page-item">
                                                            <a href="{% url 'admins:apply_port' data.next_page_number %}" aria-label="Next" class="page-link">
                                                                <span aria-hidden="true">&gt;</span>
                                                            </a>
                                                        </li>
                                                    {% else %}
                                                        <li class="disabled page-item">
                                                            <a aria-label="Next" class="page-link">
                                                                <span aria-hidden="true">&gt;</span>
                                                            </a>
                                                        </li>
                                                    {% endif %}
                                                    {% if data.number == data.paginator.num_pages %}
                                                        <li class="disabled page-item">
                                                            <a aria-label="Last" class="page-link">
                                                                <span aria-hidden="true">&raquo;</span>
                                                            </a>
                                                        </li>
                                                    {% else %}
                                                        <li class="page-item">
                                                            <a href="{% url 'admins:apply_port' data.paginator.num_pages %}" aria-label="Last" class="page-link">
                                                                <span aria-hidden="true">&raquo;</span>
                                                            </a>
                                                        </li>
                                                    {% endif %}
                                                    <span class="total" style="line-height: 30px; margin-left: 10px;">
                                                        共有{{data.paginator.count}}条记录
                                                    </span>
                                                </ul>
                                            </nav>
                                        {% endif %}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>



{% endblock %}